<?php
class M_parent_profile_updates extends MY_Model
{

	protected $_table = 'parent_profile_updates';
	protected $_timestamp = false;

	function get_student($req_id){
		$sq= 'SELECT enrollments.e_id as enrollment_id,
					 enrollments.edit_profile as edit,
					 profiles.*,
					 concat(employees.first_name," ",employees.last_name) as teacher_name,
					 enrollments.e_student_id as idno,
					 DATE_FORMAT(enrollments.e_created_at,"%M %e, %Y") as date_registered,
					 DATE_FORMAT(profiles.dob,"%M %e, %Y") as dob_formated,
					 concat(profiles.firstname," ",profiles.middlename," ",profiles.lastname) as fullname,
					 enrollments.e_grade_level as level_id,
					 enrollments.e_enrollee_type as student_type,
					 enrollments.e_section_id,
					 levels.level_desc,
					 levels.level_category,
					 sections.section_name
			  FROM parent_profile_updates as profiles
			  LEFT JOIN enrollments ON enrollments.e_id = profiles.enrollment_id
			  LEFT JOIN levels ON levels.level_id = enrollments.e_grade_level
			  LEFT JOIN sections ON sections.id = enrollments.e_section_id
			  LEFT JOIN employees ON sections.adviser_id = employees.emp_id
			  WHERE profiles.id = ?
			  LIMIT 1; 
			  ';
		$q = $this->db->query($sq,array($req_id));
		return $q->num_rows() >= 1 ? $q->row() : FALSE;
	}

	/**
	 * Create a parent request
	 * @param int $e_id Enrollment id
	 * @param int $p_id Profile id
	 * @param int $req_id Parent Profile Updates Id
	 * @return int last insert id or id of added record
	 */
	public function create_a_request($e_id, $p_id, $req_id = false)
	{
		$id = 0;

		if(empty($req_id)){

			$get['where']['profile_id'] = $p_id;
			$get['single'] = true;
			$get['array'] = true;
			$rs = $this->get_record('profiles', $get);
			if($rs){
				$data = $rs;
				$data['status'] = "PENDING";
				$data['enrollment_id'] = $e_id;
				$data['created'] = NOW;
				$data['guardian_user_id'] = $this->session->userdata['userid'];
				$add = (object)$this->insert($data);
				if($add->status){
					$id = $add->id;
				}
			}
		}else{
			
			$rs = $this->pull($req_id, 'id');
			if($rs){
				$id = $rs->id;
			}
		}

		return $id;
	}

	function create_request_personal_profile_data($_pst,$e_id= '',$p_id = '', $req_id = false)
	{
		$sly = new stdClass;
		$sly->status = false;
		$sly->msg = "Unable to save personal information. Please try again";

		$req_id = $this->create_a_request($e_id, $p_id, $req_id);
		$sly->req_id = $req_id;
		
		$profile_data['email'] = $_pst->email;
		$profile_data['firstname'] = $_pst->child_fname;
		$profile_data['middlename'] = $_pst->child_mname;
		$profile_data['lastname'] = $_pst->child_lname;
		$profile_data['nickname'] = $_pst->child_nickname;
		$profile_data['name_ext'] = $_pst->child_name_ext;
		$profile_data['pob'] = $_pst->child_placeofbirth;
		$profile_data['dob'] = DateTime::createFromFormat('m/d/Y',$_pst->child_bdate)->format('Y-m-d');
		$profile_data['gender'] = $_pst->gender;
		$profile_data['religion'] = $_pst->child_religous;
		$profile_data['nationality'] = $_pst->child_nationality;
		$profile_data['pob'] = $_pst->child_placeofbirth;
		$profile_data['present_address'] = $_pst->child_city_address;
		$profile_data['contact_no'] = $_pst->child_telno;
		$profile_data['mobile_phone'] = $_pst->mobile_phone;
		$profile_data['religion'] = $_pst->child_religous;
		$profile_data['living_in_ph_with_father'] = !isset($_pst->living_in_ph_with_father) ? 'no' : 'yes';
		$profile_data['living_in_ph_with_mother'] = !isset($_pst->living_in_ph_with_mother) ? 'no' : 'yes';
		$profile_data['living_in_ph_with_step_father'] = !isset($_pst->living_in_ph_with_step_father) ? 'no' : 'yes';
		$profile_data['living_in_ph_with_step_mother'] = !isset($_pst->living_in_ph_with_step_mother) ? 'no' : 'yes';
		$profile_data['living_in_ph_with_guardian'] = !isset($_pst->living_in_ph_with_guardian) ? 'no' : 'yes';
		$profile_data['guardian_name'] = $_pst->guardian_name;
		$profile_data['guardian_mname'] = $_pst->guardian_mname;
		$profile_data['guardian_lname'] = $_pst->guardian_lname;
		$profile_data['guardian_citizenship'] = $_pst->guardian_citizenship;
		$profile_data['guardian_visa'] = $_pst->guardian_visa;
		$profile_data['guardian_occup'] = $_pst->guardian_occup;
		$profile_data['guardian_office_tel'] = $_pst->guardian_office_tel;
		$profile_data['guardian_contact_no'] = $_pst->guardian_contact_no;
		$profile_data['childs_english_reading_writing_level'] = $_pst->childs_english_reading_writing_level;
		$profile_data['childs_english_verbal_proficiency'] = $_pst->childs_english_verbal_proficiency;
		$profile_data['language_at_home'] = $_pst->language_at_home;
		$profile_data['participation_in_advanced_level_classes'] = $_pst->participation_in_advanced_level_classes;
		$profile_data['remedial_help_in_previous_schools'] = $_pst->remedial_help_in_previous_schools;
		$profile_data['special_talent_interest_dance'] = !isset($_pst->special_talent_interest_dance) ? 'no' : 'yes';
		$profile_data['special_talent_interest_drama'] = !isset($_pst->special_talent_interest_drama) ? 'no' : 'yes';
		$profile_data['special_talent_interest_art'] = !isset($_pst->special_talent_interest_art) ? 'no' : 'yes';
		$profile_data['special_talent_interest_acomputer'] = !isset($_pst->special_talent_interest_acomputer) ? 'no' : 'yes';
		$profile_data['special_talent_interest_vocal_chorus'] = !isset($_pst->special_talent_interest_vocal_chorus) ? 'no' : 'yes';
		$profile_data['special_talent_interest_athletics'] = !isset($_pst->special_talent_interest_athletics) ? 'no' : 'yes';
		$profile_data['special_talent_interest_athletics_spec'] = $_pst->special_talent_interest_athletics_spec;
		$profile_data['special_talent_interest_band'] = !isset($_pst->special_talent_interest_band) ? 'no' : 'yes';
		$profile_data['special_talent_interest_band_spec'] = $_pst->special_talent_interest_band_spec;
		$profile_data['special_talent_interest_string'] = !isset($_pst->special_talent_interest_string) ? 'no' : 'yes';
		$profile_data['special_talent_interest_string_spec'] = $_pst->special_talent_interest_string_spec;
		$profile_data['dhc_hinder_learning_process'] = $_pst->hinder_childs_learning;
		$profile_data['leave_school'] = $_pst->leave_school;
		$profile_data['previous_school'] = $_pst->previous_school;
		$profile_data['previous_school_address'] = $_pst->previous_school_address;
		$profile_data['previous_grade_level'] = $_pst->previous_grade_level;
		$profile_data['name_of_siblings_and_school_attended'] = $_pst->name_of_siblings_and_school_attended;

		$u = $this->_update_request($req_id, $profile_data,$p_id);

		if($u){
			$sly->status = true;
			$sly->msg = "Your request has been created. This will be submitted to the registrar. You may continue to edit other tab information.";
		}

		return $sly;
	}

	function create_request_parent_legal_information_data($_pst,$e_id= '',$p_id = '',  $req_id = false)
	{
		$sly = new stdClass;
		$sly->status = false;
		$sly->msg = "Unable to save parent legal information. Please try again.";

		$req_id = $this->create_a_request($e_id, $p_id, $req_id);
		$sly->req_id = $req_id;

		$profile_data['fathername'] = $_pst->father_name;
		$profile_data['father_mname'] = $_pst->father_mname;
		$profile_data['father_lname'] = $_pst->father_lname;
		$profile_data['father_nationality'] = $_pst->father_citizenship;
		$profile_data['father_visa'] = $_pst->father_visa;
		$profile_data['father_occupation'] = $_pst->father_occup;
		$profile_data['father_office_num'] = $_pst->father_office_tel;
		$profile_data['father_contact_no'] = $_pst->father_contact_no;
		$profile_data['father_contact_no_text'] = !isset($_pst->father_contact_no_text) ? 'no' : 'yes';
		$profile_data['father_graduate_degree'] = $_pst->father_graduate_degree;
		$profile_data['father_school'] = $_pst->father_school;
		$profile_data['father_income_bracket'] = $_pst->father_income_bracket;
		$profile_data['mothername'] = $_pst->mother_name;
		$profile_data['mother_mname'] = $_pst->mother_mname;
		$profile_data['mother_lname'] = $_pst->mother_lname;
		$profile_data['mother_nationality'] = $_pst->mother_citizenship;
		$profile_data['mother_visa'] = $_pst->mother_visa;
		$profile_data['mother_occupation'] = $_pst->mother_occup;
		$profile_data['mother_office_num'] = $_pst->mother_office_tel;
		$profile_data['mother_contact_no'] = $_pst->mother_contact_no;
		$profile_data['mother_contact_no_text'] = !isset($_pst->mother_contact_no_text) ? 'no' : 'yes';
		$profile_data['mother_graduate_degree'] = $_pst->mother_graduate_degree;
		$profile_data['mother_school'] = $_pst->mother_school;
		$profile_data['mother_income_bracket'] = $_pst->mother_income_bracket;
		$profile_data['step_father_name'] = $_pst->step_father_name;
		$profile_data['step_father_mname'] = $_pst->step_father_mname;
		$profile_data['step_father_lname'] = $_pst->step_father_lname;
		$profile_data['step_father_citizenship'] = $_pst->step_father_citizenship;
		$profile_data['step_father_visa'] = $_pst->step_father_visa;
		$profile_data['step_father_occup'] = $_pst->step_father_occup;
		$profile_data['step_father_office_tel'] = $_pst->step_father_office_tel;
		$profile_data['step_father_contact_no'] = $_pst->step_father_contact_no;
		$profile_data['step_father_contact_no_text'] = !isset($_pst->step_father_contact_no_text) ? 'no' : 'yes';
		$profile_data['step_father_graduate_degree'] = $_pst->step_father_graduate_degree;
		$profile_data['step_father_school'] = $_pst->step_father_school;
		$profile_data['step_father_income_bracket'] = $_pst->step_father_income_bracket;
		$profile_data['step_mother_name'] = $_pst->step_mother_name;
		$profile_data['step_mother_mname'] = $_pst->step_mother_mname;
		$profile_data['step_mother_lname'] = $_pst->step_mother_lname;
		$profile_data['step_mother_citizenship'] = $_pst->step_mother_citizenship;
		$profile_data['step_mother_visa'] = $_pst->step_mother_visa;
		$profile_data['step_mother_occup'] = $_pst->step_mother_occup;
		$profile_data['step_mother_office_tel'] = $_pst->step_mother_office_tel;
		$profile_data['step_mother_contact_no'] = $_pst->step_mother_contact_no;
		$profile_data['step_mother_contact_no_text'] = !isset($_pst->step_mother_contact_no_text) ? 'no' : 'yes';
		$profile_data['step_mother_graduate_degree'] = $_pst->step_mother_graduate_degree;
		$profile_data['step_mother_school'] = $_pst->step_mother_school;
		$profile_data['step_mother_income_bracket'] = $_pst->step_mother_income_bracket;
		$profile_data['failure_to_provide'] = !isset($_pst->failure_to_provide) ? 'no' : 'yes';
		$profile_data['to_the_best'] = !isset($_pst->to_the_best) ? 'no' : 'yes';

		// $this->db->set($profile_data)->where('profile_id',$p_id)->update('profiles');
		// return $this->db->affected_rows() >= 1 ? TRUE : FALSE;

		$u = $this->_update_request($req_id, $profile_data,$p_id);

		if($u){
			$sly->status = true;
			$sly->msg = "Your request has been created. This will be submitted to the registrar. You may continue to edit other tab information.";
		}

		return $sly;
	}

	function create_request_health_history_data($_pst,$e_id= '',$p_id = '', $req_id = false)
	{
		$sly = new stdClass;
		$sly->status = false;
		$sly->msg = "Unable to save health history. Please try again";

		$req_id = $this->create_a_request($e_id, $p_id, $req_id);
		$sly->req_id = $req_id;

		$profile_data['hhc_allergies_reaction'] = $_pst->allergies_reaction;
		$profile_data['hhc_alergic_med_reaction'] = $_pst->drug_allergies_reaction;
		$profile_data['dhc_sight_problems_what'] = $_pst->eye_problem_spec;
		$profile_data['dhc_hearing_problems_what'] = $_pst->ear_problem_spec;
		$profile_data['hhc_diagnosed_specify'] = $_pst->major_ailment;
		$profile_data['hhc_recently_hospitalized'] = $_pst->recently_hospitalized;
		$profile_data['hhc_major_injury_specify'] = $_pst->major_injury;
		$profile_data['hhc_medication_school_hours'] = $_pst->medication_child_taking_spec;
		$profile_data['hhc_asthma_inhaler'] = isset($_pst->asthma_inhaler) ? $_pst->asthma_inhaler : 'no';
		$profile_data['hhc_asthma'] = isset($_pst->asthma) ? $_pst->asthma : 'no';

		$u = $this->_update_request($req_id, $profile_data,$p_id);

		if($u){
			$sly->status = true;
			$sly->msg = "Your request has been created. This will be submitted to the registrar. You may continue to edit other tab information.";
		}

		return $sly;
	}
	
	function create_request_authorization_data($_pst,$e_id= '',$p_id = '', $req_id = false)
	{

		$sly = new stdClass;
		$sly->status = false;
		$sly->msg = "Unable to save authorization. Please try again";

		$req_id = $this->create_a_request($e_id, $p_id, $req_id);
		$sly->req_id = $req_id;

		$profile_data['auth_firstaid'] = isset($_pst->authfirst_aid) ? $_pst->authfirst_aid : NULL;
		$profile_data['auth_emergency_care'] = isset($_pst->auth_emergency) ? $_pst->auth_emergency : NULL;
		$profile_data['auth_emergency_care_hospital'] = isset($_pst->auth_emergency_nearest_hosp) ? $_pst->auth_emergency_nearest_hosp : NULL;
		$profile_data['auth_oral_non_presc'] = isset($_pst->auth_oral_non_presc) ? $_pst->auth_oral_non_presc : NULL;
		$profile_data['first_permission'] = isset($_pst->first_permission) ? $_pst->first_permission : NULL;
		$profile_data['second_permission'] = isset($_pst->second_permission) ? $_pst->second_permission : NULL;
		$profile_data['third_permission'] = isset($_pst->third_permission) ? $_pst->third_permission : NULL;

		$u = $this->_update_request($req_id, $profile_data,$p_id);

		if($u){
			$sly->status = true;
			$sly->msg = "Your request has been created. This will be submitted to the registrar. You may continue to edit other tab information.";
		}

		return $sly;
	}

	/**
	 * Update Record 
	 * - And insert to parent_request_field - this is a table for each column that has been updated
	 */
	public function _update_request($id, $data = array(),$p_id)
	{
		$this->load->model('M_parent_request_field','m_f');
		$this->load->model('M_profiles','m_p');

		$upd = $this->update($id, $data); //UPDATE THE RECORD
		// vd($upd);
		if($upd){

			$flds = array();
			foreach ($data as $f => $v){ $flds[] = $f; } //Get all fields 

			$profile = $this->m_p->pull($p_id, $flds);
			$profile_request = $this->pull($id, $flds);
			// vd($data);
			foreach ($data as $f => $v) {
				
				//Compare the value from  profile to parent_profile_updates, if different then save it to parent_request_field
				if($profile && $profile_request){
					if($profile->$f != $profile_request->$f){

						// Save but check first if exists, if yes then edit
						unset($save);
						$save['ppu_id'] = $id;
						$save['col'] = $f;
						$save['value'] = $v;
						$save['previous_value'] = $profile->$f;

						$get = array('ppu_id'=>$id,'col'=>$f);
						$added_already = $this->m_f->pull($get,'id');
						if($added_already){
							$this->m_f->update($added_already->id, $save);
						}else{
							$this->m_f->insert($save);
						}
					}
				}
			}

			return true;
		}

		return false;
	}

	/**
	 * Get All Update Request
	 */
	public function get_all_request()
	{
		$sql= 'SELECT 
					 profiles.id,
					 profiles.status,
					 profiles.status_by,
					 profiles.status_date,
					 profiles.created,
					 profiles.reason,
					 enrollments.e_id as enrollment_id,
					 enrollments.edit_profile as edit,
					 concat(profiles.firstname," ",profiles.middlename," ",profiles.lastname) as full_name,
					 concat(employees.first_name," ",employees.last_name) as teacher_name,
					 enrollments.e_student_id as idno,
					 DATE_FORMAT(enrollments.e_created_at,"%M %e, %Y") as date_registered,
					 DATE_FORMAT(profiles.dob,"%M %e, %Y") as dob_formated,
					 concat(profiles.firstname," ",profiles.middlename," ",profiles.lastname) as fullname,
					 enrollments.e_grade_level as level_id,
					 enrollments.e_enrollee_type as student_type,
					 enrollments.e_section_id,
					 levels.level_desc,
					 levels.level_category,
					 sections.section_name
			  FROM parent_profile_updates as profiles
			  LEFT JOIN enrollments ON enrollments.e_id = profiles.enrollment_id
			  LEFT JOIN levels ON levels.level_id = enrollments.e_grade_level
			  LEFT JOIN sections ON sections.id = enrollments.e_section_id
			  LEFT JOIN employees ON sections.adviser_id = employees.emp_id
			  WHERE profiles.guardian_user_id = ?
			  ORDER BY created DESC 
			  ';

		return $this->query($sql,[$this->session->userdata['userid']]);
	}

	/**
	 * Check if Value Updated 
	 */
	public function check_if_value_updated($r_id, $field, $value)
	{
		$this->load->model(['M_profiles','M_parent_profile_updates']);

		$pid = $r_id[0]; //profile_id
		$rid = $r_id[1]; //profile request id

		$profiles = $this->M_profiles->pull(array('profile_id'=>$pid),$field);
		$updates = $this->M_parent_profile_updates->pull($rid,$field);

		if($profiles && $updates)	{
			return $profiles->$field != $updates->$field ? true : false;
		}

		return false;
	}


	/**
	 * Delete Record
	 * @param int $id
	 */
	public function delete_request($id)
	{
		$this->load->model('M_parent_request_field','mf');

		$req = $this->mf->delete(array('ppu_id'=>$id));

		$rs = $this->delete($id);

		return $rs;
	}
}

